package com.zhuoyue.web.controller;


import java.util.List;

import com.zhuoyue.biz.core.api.dto.rep.LocalSceneRepDTO;

import com.zhuoyue.biz.core.api.service.impl.LocalSceneService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import org.apache.commons.collections.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.zhuoyue.biz.core.api.domain.LocalSceneDO;
import com.zhuoyue.biz.core.api.domain.ShowSceneDO;

import com.zhuoyue.common.constant.SceneConstant;

import com.zhuoyue.common.dto.ControllerResult;

import javax.validation.constraints.NotBlank;


/**
 * @author wjming
 * @ClassName: AppShowSceneController
 * @Description:场景展示控制接口
 * @Date 2021/4/17 13:41
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/show/scene")
@Validated
public class AppShowSceneController {

    @Autowired
    private LocalSceneService localSceneService;

    /**
     * @Description: 获取首页展示场景
     * @param: homeId
     * @return: com.zhuoyue.common.dto.ControllerResult
     * @author:wjming
     * @Date:2021/4/17 13:41
     */
    @GetMapping("/list")
    @CheckLogin
    public ControllerResult getShowSceneList(@RequestParam @NotBlank(message = "家庭ID不能为空！") String homeId) {

        List<ShowSceneDO> lsRes = localSceneService.getShowLocalSceneListByHomeId(homeId);

        if (CollectionUtils.isEmpty(lsRes)) {
            return ControllerResult.toNoDataFail("用户未设置首页展示的场景！");
        }

        return ControllerResult.toReturnDataSuccess("查询成功", lsRes);
    }

    /**
     * 修改首页展示场景
     * @param sceneId   场景ID
     * @param sceneType 场景类型
     * @param dealType  处理类型
     * @return
     */
    @PutMapping("/deal")
    @CheckLogin
    public ControllerResult dealShowScene(@NotBlank(message = "场景ID不能为空！") String sceneId,
                                          @NotNull("场景类型不能为空！") Integer sceneType,
                                          @NotBlank(message = "首页展示场景处理类型不能为空！") String dealType) {
        if (!dealType.equals(SceneConstant.HomePageSceneDealType.ADD_SHOW) && !dealType.equals(SceneConstant.HomePageSceneDealType.REMOVE_SHOW)) {
            return ControllerResult.toParamFail("首页展示场景处理类型参数错误！");
        }
        if (dealType.equals(SceneConstant.HomePageSceneDealType.ADD_SHOW)) {
            LocalSceneDO localScene = new LocalSceneDO();
            localScene.setId(sceneId);
            localScene.setIsShow(SceneConstant.SceneIsShow.YES);
            localSceneService.updateById(localScene);
        } else if (dealType.equals(SceneConstant.HomePageSceneDealType.REMOVE_SHOW)) {
            LocalSceneDO localScene = new LocalSceneDO();
            localScene.setId(sceneId);
            localScene.setIsShow(SceneConstant.SceneIsShow.NO);
            localSceneService.updateById(localScene);
        }
        LocalSceneRepDTO localSceneById = localSceneService.getLocalSceneById(sceneId);
        return ControllerResult.toReturnDataSuccess("成功", localSceneById);
    }


}
